//
//  AlertViewController.m
//  helloword
//
//  Created by chen on 17/2/24.
//  Copyright © 2017年 chen. All rights reserved.
//

#import "AlertViewController.h"
#import "Commen.h"

@interface AlertViewController ()

@property (strong, nonatomic) IBOutlet UIActivityIndicatorView *activityIdicatorView;
@property (strong,nonatomic) NSTimer *timer;
@property (strong, nonatomic) IBOutlet UIDatePicker *datePicker;
@property (strong, nonatomic) IBOutlet UIProgressView *progressView;
@end

@implementation AlertViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initWithFrame];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark -- initWithFrame初始化
- (void)initWithFrame{
    CGRect screen = [[UIScreen mainScreen] bounds];
    //添加AlertView按钮
    UIButton *buttonAlertView = [UIButton buttonWithType:UIButtonTypeSystem];
    [buttonAlertView setTitle:@"Test警示框" forState:UIControlStateNormal];
    CGFloat buttonAlertViewWidth = 100;
    CGFloat buttonAlertViewHeight = 30;
    CGFloat buttonAlertTopView = 130;
    
    buttonAlertView.frame = CGRectMake((screen.size.width - buttonAlertViewWidth)/2, buttonAlertTopView, buttonAlertViewWidth, buttonAlertViewHeight);
    //增加事件处理方法
    [buttonAlertView addTarget:self action:@selector(testAlertView:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:buttonAlertView];
    //添加ActionSheet按钮
    UIButton *buttonActionSheet = [UIButton buttonWithType:UIButtonTypeSystem];
    [buttonActionSheet setTitle:@"Test操作框" forState:UIControlStateNormal];
    CGFloat buttonActionSheetWidth = 100;
    CGFloat buttonActionSheetHeight = 30;
    CGFloat buttonActionSheetTopView = 230;
    
    buttonActionSheet.frame = CGRectMake((screen.size.width - buttonActionSheetWidth)/2, buttonActionSheetTopView, buttonActionSheetWidth, buttonActionSheetHeight);
    //增加事件处理方法
    [buttonActionSheet addTarget:self action:@selector(testActionSheet:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:buttonActionSheet];
}
-(void)testAlertView:(id)sender{
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Alert" message:@"Alert text goes here" preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *noAction = [UIAlertAction actionWithTitle:@"NO" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        NSLog(@"Tap No Button");
    }];
    UIAlertAction *yesAction = [UIAlertAction actionWithTitle:@"YES" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        NSLog(@"Tap Yes Button");
    }];
    [alertController addAction:noAction];
    [alertController addAction:yesAction];
    
    //显示动画
    [self presentViewController:alertController animated:true completion:nil];
    
}
-(void)testActionSheet:(id)sender{
    UIAlertController *actionSheetController = [[UIAlertController alloc]init];
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        NSLog(@"Tap 取消Button");
    }];
    UIAlertAction *destructiveAction = [UIAlertAction actionWithTitle:@"破坏性按钮" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        NSLog(@"Tap 破坏性按钮Button");
    }];
    UIAlertAction *sinaAction = [UIAlertAction actionWithTitle:@"新浪微博" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        NSLog(@"Tap 新浪微博Button");
    }];
    [actionSheetController addAction:cancelAction];
    [actionSheetController addAction:destructiveAction];
    [actionSheetController addAction:sinaAction];
    
    [self presentViewController:actionSheetController animated:true completion:nil];
    
}
#pragma mark -- ActivityIndicatorView
- (IBAction)startToMove:(id)sender {
    if (_activityIdicatorView.isAnimating) {
        [_activityIdicatorView stopAnimating];
    }else{
        [_activityIdicatorView startAnimating];
    }
}
- (IBAction)downloadProgress:(id)sender {
    _progressView.progress = 0.0;
    _timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(download) userInfo:nil repeats:YES];
}
-(void)download{
    _progressView.progress = _progressView.progress + 0.1;
    //这个时间是选择器的时间
    NSDate *pickerDate = _datePicker.date;
    //这个时间是当前的时间
    NSDate *startDate = [[NSDate alloc]init];
    NSDate *endDate;
    //时间戳[[NSDate date] timeIntervalSince1970]
    
    NSTimeInterval startTimer = [[NSDate date] timeIntervalSince1970];
    NSTimeInterval endTimer = [[NSDate date] timeIntervalSince1970];
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"];
    if (_progressView.progress == 1.0) {
        endDate = [[NSDate alloc]init];
        [_timer invalidate];
        NSTimeInterval time = [endDate timeIntervalSinceDate:startDate];
        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"download completed" message:[Commen dateTimeDifferenceWithStartTime:@"2017-02-25 10:24:40" endTime:[formatter stringFromDate:pickerDate]] preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleCancel handler:nil];
        [alertController addAction:okAction];
        //显示动画
        [self presentViewController:alertController animated:true completion:nil];
    }
    
}



/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
